import { WebviewWindow } from '@tauri-apps/api/webviewWindow'
import {
  plainGlyph,
  plainCharacter,
  plainFile,
  mapToObject,
  instanceGlyph,
  instanceCharacter,
  getProjectData,
  saveFile_tauri,
  saveAs_tauri,
  base64ToArrayBuffer,
  nativeSaveText,
  nativeSaveBinary,
  addLoaded,
  nativeImportFile,
  nativeImportTextFile,
  createFile,
  openFile as openFileFromFileHandlers,
  openFile_tauri,
  saveFile_web as saveFileWebFromFileHandlers,
  saveFile as saveFileFromFileHandlers,
  clearCache as clearCacheFromFileHandlers,
  syncData as syncDataFromFileHandlers,
  _syncData as syncDataInternalFromFileHandlers,
  __openFile as openFileInternalFromFileHandlers,
  _openFile as openFileWebInternalFromFileHandlers,
  exportJSON as exportJSONFromFileHandlers,
} from './fileHandlers'
import { undo, redo, copy, paste, cut, del } from './editHandlers'
import { addCharacter, addIcon } from './characterHandlers'
import { fontSettings, preferenceSettings, languageSettings } from './settingsHandlers'
import {
  importFont_tauri,
  importFont,
  importGlyphs_tauri,
  importGlyphs,
  importSVG_tauri,
  importSVG,
  importPic,
  importPic_tauri,
  getFromFontPic,
  getFromPic,
  thumbnail,
} from './importHandlers'
import {
  computeOverlapRemovedContours,
  computeOverlapRemovedContours_wasm,
  exportFont_tauri,
  exportFont,
  exportColorFont,
  exportColorFont_tauri,
  exportVarFont,
  exportVarFont_tauri,
  exportJPEG,
  exportJPEG_tauri,
  exportPNG,
  exportPNG_tauri,
  exportSVG,
  exportSVG_tauri,
  exportGlyphs,
  exportGlyphs_tauri,
  showExportFontDialog_tauri,
  showExportFontDialog,
  showExportColorFontDialog_tauri,
  showExportColorFontDialog,
  showExportVarFontDialog_tauri,
  showExportVarFontDialog,
} from './exportHandlers'
import {
  removeOverlap,
  formatAllCharacters,
  formatCurrentCharacter,
  generateCharFile,
  generateComponent,
  runFormatAllCharacters,
  runFormatCurrentCharacter,
} from './toolsHandlers'
import {
  importTemplates,
  importTemplate1,
  importTemplate2,
  importTemplate3,
  importTemplate4,
  importTemplate5,
  importTemplate6,
  importTemplate7,
  importTemplate8,
  importTemplateDigits,
  importTemplateLetters,
} from './templatesHandlers'

const openPlayground = () => {
  //@ts-ignore
  if (window.__TAURI_INTERNALS__) {
    const windowOptions = {
      url: `${location.origin}${location.pathname}#/playground`,
      width: 1280,
      height: 800,
      x: (screen.width - 1280) / 2,
      y: (screen.height - 800) / 2,
      //devtools: true,
    }
    const webview = new WebviewWindow('glyph-script', windowOptions)
    webview.once('tauri://created', async () => {
      console.log('webview created')
    })
    webview.once('tauri://error', function (e) {
      console.log('error creating webview', e)
    })
  } else {
    const playground_window = window.open(
      // `${location.origin}${base}/#/glyph-programming-editor`,
      `${location.origin}${location.pathname}#/playground`,
      'playground',
      `popup,width=${1280},height=${800},left=${(screen.width - 1280) / 2}`,
    )
  }
}

const tauri_handlers: IHandlerMap = {
  'create-file': createFile,
  'open-file': openFile_tauri,
  'save-file': saveFile_tauri,
  'save-as': saveAs_tauri,
  'undo': undo,
  'redo': redo,
  'cut': cut,
  'copy': copy,
  'paste': paste,
  'delete': del,
  'import-font-file': importFont_tauri,
  'import-templates-file': importTemplates,
  'import-glyphs': importGlyphs_tauri,
  'import-pic': importPic_tauri,
  'import-svg': importSVG_tauri,
  'export-font-file': showExportFontDialog_tauri,
  'export-var-font-file': showExportVarFontDialog_tauri,
  'export-color-font': showExportColorFontDialog_tauri,
  'export-glyphs': exportGlyphs_tauri,
  'export-jpeg': exportJPEG_tauri,
  'export-png': exportPNG_tauri,
  'export-svg': exportSVG_tauri,
  'add-character': addCharacter,
  'add-icon': addIcon,
  'font-settings': fontSettings,
  'preference-settings': preferenceSettings,
  'language-settings': languageSettings,
  'template-1': importTemplate1,
  'template-2': importTemplate2,
  'template-3': importTemplate3,
  'template-4': importTemplate4,
  'template-5': importTemplate5,
  'template-6': importTemplate6,
  'template-7': importTemplate7,
  'template-8': importTemplate8,
  'template-digits': importTemplateDigits,
  'template-letters': importTemplateLetters,
  'remove_overlap': removeOverlap,
  'format-all-characters': formatAllCharacters,
  'format-current-character': formatCurrentCharacter,
}

const web_handlers: IHandlerMap = {
  'create-file': createFile,
  'open-file': openFileFromFileHandlers,
  'save-file': saveFileWebFromFileHandlers,//saveFile,
  'clear-cache': clearCacheFromFileHandlers,
  'sync-data': syncDataFromFileHandlers,
  'save-as-json': exportJSONFromFileHandlers,
  'undo': undo,
  'redo': redo,
  'cut': cut,
  'copy': copy,
  'paste': paste,
  'delete': del,
  'import-font-file': importFont,
  'import-templates-file': importTemplates,
  'import-glyphs': importGlyphs,
  'import-pic': importPic,
  'import-svg': importSVG,
  'export-font-file': showExportFontDialog,
  'export-var-font-file': showExportVarFontDialog,
  'export-color-font': showExportColorFontDialog,
  'export-glyphs': exportGlyphs,
  'export-jpeg': exportJPEG,
  'export-png': exportPNG,
  'export-svg': exportSVG,
  'add-character': addCharacter,
  'add-icon': addIcon,
  'font-settings': fontSettings,
  'preference-settings': preferenceSettings,
  'language-settings': languageSettings,
  'template-1': importTemplate1,
  'template-2': importTemplate2,
  'template-3': importTemplate3,
  'template-4': importTemplate4,
  'template-5': importTemplate5,
  'template-6': importTemplate6,
  'template-7': importTemplate7,
  'template-8': importTemplate8,
  'template-digits': importTemplateDigits,
  'template-letters': importTemplateLetters,
  'remove_overlap': removeOverlap,
  'format-all-characters': formatAllCharacters,
  'format-current-character': formatCurrentCharacter,
}

interface IHandlerMap {
  [key: string]: Function;
}

export {
  web_handlers,
  instanceGlyph,
  createFile,
  openFileFromFileHandlers as openFile,
  syncDataFromFileHandlers as syncData,
  saveFileFromFileHandlers as saveFile,
  plainFile,
  plainGlyph,
  mapToObject,
  importTemplate1,
  importTemplate2,
  importFont,
  exportFont,
  exportFont_tauri,
  computeOverlapRemovedContours,
  tauri_handlers,
  nativeImportFile,
  instanceCharacter,
  nativeSaveBinary,
  openPlayground,
  addLoaded,
  exportVarFont,
  exportColorFont,
  exportVarFont_tauri,
  exportColorFont_tauri,
  exportJSONFromFileHandlers as exportJSON,
}